<template>
  <div class="recommen-list">
    <scroller :noDataText="noDataText" :on-refresh="refresh" :on-infinite="infinite" style="padding-top:95px;min-height: 500px;" ref="my_scroller">
      <item-list @remove="handleRemove" :datalist="list" @focusman="handleFocusman" :hasLikeArr="[]" :hasAttenArr="[]"></item-list>
  </scroller>
  <actionsheet
    v-model="showConfirm"
    :menus="cacelList"
    show-cancel
    @on-click-menu="clickMenu">
  </actionsheet>
  </div>
</template>
<script>
import { Vue, Component } from 'vue-property-decorator';
import { Actionsheet } from 'vux';
import { namespace } from 'vuex-class';
import ItemList from '../common/Item';
import { getUnixTime } from '../../js/utils'

const IndexStore = namespace('list');
const HomeStore = namespace('home');
@Component({
  components: { Actionsheet, ItemList }
})
export default class RecommenList extends Vue {
  @IndexStore.Action getPostList;
  @IndexStore.State postList;
  @HomeStore.Action getLikePost;
  @HomeStore.Action getWatchUser;
  @HomeStore.Action getSaveCollect;
  @HomeStore.Action getUnWatchPost;
  currentItem = null
  showConfirm = false
  cacelList = {
  	0: '不再看到此条帖子',
  	1: '收藏',
    2: '举报'
    // 3: '不看此人'
  }
  list = []
  currentPage = 1
  pageSize = 10
  handleFocusman (item) {
    const params = {
      status: 0,
      user2Obj: item.creator
    }
    this.getWatchUser(params).then(msg => {
      if (msg) {
        this.$vux.toast.text(msg, 'middle');
      } else {
        this.$vux.toast.text('关注成功', 'middle');
      }
    })
  }
  noDataText = '--我也是有底线的--'
  infinite () {
    this.initPostList()
    this.currentPage++
    setTimeout(() => {
      this.$refs.my_scroller.finishInfinite(true)
    }, 1000)
  }
  myTime = new Date()
  refresh () {
    this.myTime = new Date()
    setTimeout(() => {
      this.list = []
      this.currentPage = 1
      this.$refs.my_scroller.finishPullToRefresh()
    }, 1000)
  }
  handleRemove (item) {
    this.currentItem = item
    this.id = item._id
    this.showConfirm = true
  }
  userInfo = (localStorage.getItem('loginInfo') && JSON.parse(localStorage.getItem('loginInfo')).user) || ''
  clickMenu (key, value) {
    if (key === '2') {
      const { creator, _id } = this.currentItem
      const info = {
        _id,
        creator,
        type: 'post'
      }
      this.$router.push({
        path: `/report`,
        query: {
          info
        }
      })
    }
    if (key === '1') {
      const params = {
        postId: this.id,
        userId: this.userInfo._id
      }
      this.getSaveCollect(params).then(msg => {
        if (msg) {
          this.$vux.toast.text(msg, 'middle');
        } else {
          this.$vux.toast.text('收藏成功', 'middle');
        }
      })
    }
    if (key === '0') {
      const params = {
        postId: this.id
      }
      this.getUnWatchPost(params).then(msg => {
        if (msg) {
          this.$vux.toast.text(msg, 'middle');
        } else {
          this.$vux.toast.text('已不再看到此帖子', 'middle');
        }
      })
    }
  }
  // created () {
  //   this.initPostList()
  // }
  initPostList () {
    const params = {
      current: this.currentPage,
      createTime: getUnixTime(this.myTime),
      pageSize: this.pageSize,
      userId: this.userInfo && this.userInfo._id
    }
    this.getPostList(params).then(msg => {
      if (msg) {
        this.$vux.toast.text(msg, 'middle');
      } else {
        const { list } = this.postList
        this.list = this.list.concat(list)
      }
    })
  }
}
</script>
